package com.baishui.test;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.StringUtils;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class App {
        private static final Map<String, String> listMarkerCache = new HashMap<>();

        public static void main(String[] args) throws Exception {
            // 这里是你的JSON字符串（实际使用时替换为完整JSON）
            String jsonContent = new String(Files.readAllBytes(Paths.get("C:\\Users\\Administrator\\Desktop\\20250625213653-es97nx3.txt")));

            ObjectMapper mapper = new ObjectMapper();
            JsonNode rootNode = mapper.readTree(jsonContent);

            StringBuilder markdown = new StringBuilder();
            processNode(rootNode, markdown, 0);
            System.out.println(markdown.toString());
        }

        private static void processNode(JsonNode node, StringBuilder markdown, int listLevel) {
            if (!node.isObject()) return;

            ObjectNode objNode = (ObjectNode) node;
            String type = objNode.path("Type").asText("");

            switch (type) {
                case "NodeDocument":
                    processChildren(objNode, markdown, listLevel);
                    break;

                case "NodeHeading":
                    int level = objNode.path("HeadingLevel").asInt(1);
                    markdown.append("\n").append(StringUtils.repeat("#",level)).append(" ");
                    processChildren(objNode, markdown, listLevel);
                    markdown.append("\n");
                    break;

                case "NodeParagraph":
                    processChildren(objNode, markdown, listLevel);
//                    markdown.append("\n");
                    break;

                case "NodeList":
                    processList(objNode, markdown, listLevel);
                    break;

                case "NodeListItem":
                    processListItem(objNode, markdown, listLevel);
                    break;

                case "NodeText":
                    markdown.append(objNode.path("Data").asText(""));
                    break;

                case "NodeTextMark":
                    String markType = objNode.path("TextMarkType").asText("");
                    String content = objNode.path("TextMarkTextContent").asText("");
                    if ("strong".equals(markType)) {
                        markdown.append("**").append(content).append("**");
                    } else {
                        markdown.append(content);
                    }
                    break;

                case "NodeImage":
                    String altText = "image";
                    String imageUrl = "";

                    if (objNode.has("Children")) {
                        for (JsonNode child : objNode.path("Children")) {
                            if (child.path("Type").asText("").equals("NodeLinkDest")) {
                                imageUrl = child.path("Data").asText("");
                            } else if (child.path("Type").asText("").equals("NodeLinkText")) {
                                altText = child.path("Data").asText("");
                            }
                        }
                    }
                    markdown.append("![").append(altText).append("](").append(imageUrl).append(")");
                    break;

                default:
                    processChildren(objNode, markdown, listLevel);
                    break;
            }
        }

        private static void processList(ObjectNode listNode, StringBuilder markdown, int listLevel) {
            int newLevel = listLevel + 1;
            JsonNode children = listNode.path("Children");
            if (children.isArray()) {
                for (JsonNode child : children) {
                    processNode(child, markdown, newLevel);
                }
            }
        }

        private static void processListItem(ObjectNode itemNode, StringBuilder markdown, int listLevel) {
            // 处理列表标记
            String marker = "";
            if (itemNode.has("ListData")) {
                JsonNode listData = itemNode.path("ListData");
                String markerBase64 = listData.path("Marker").asText("");
                if (!markerBase64.isEmpty()) {
                    marker = listMarkerCache.computeIfAbsent(markerBase64, k ->
                            new String(Base64.getDecoder().decode(markerBase64)));
                }
            }

            // 添加缩进
            markdown.append("\n").append(StringUtils.repeat("  ",listLevel - 1));
            markdown.append(marker).append(" ");

            // 处理子节点
            processChildren(itemNode, markdown, listLevel);
        }

        private static void processChildren(JsonNode node, StringBuilder markdown, int listLevel) {
            if (!node.has("Children")) return;

            JsonNode children = node.path("Children");
            if (children.isArray()) {
                for (JsonNode child : children) {
                    processNode(child, markdown, listLevel);
                }
            }
        }
}
